package com.prevpn.controller;

import com.prevpn.common.CommonResponse;
import com.prevpn.dto.RoleAssignResourceDTO;
import com.prevpn.dto.RoleCreateDTO;
import com.prevpn.service.RoleService;
import com.prevpn.vo.RoleDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     * POST /role/create
     */
    @PostMapping("/create")
    public CommonResponse<Map<String, Object>> createRole(@RequestBody RoleCreateDTO dto) {
        return roleService.createRole(dto);
    }

    /**
     * 查询角色详情（基于角色名称）
     * GET /role/queryByName?directoryDomain=local&name=role1
     */
    @GetMapping("/queryByName")
    public CommonResponse<RoleDetailVO> queryRoleByName(
            @RequestParam String directoryDomain,
            @RequestParam String name,
            @RequestParam(required = false) Integer type
    ) {
        return roleService.queryRoleByName(directoryDomain, name, type);
    }

    /**
     * 角色授权应用（基于名称）
     */
    @PostMapping("/assignResourceByName")
    public CommonResponse<Object> assignResourceByName(@RequestBody RoleAssignResourceDTO dto) {
        return roleService.assignResourceByName(dto);
    }

    /**
     * 查询角色列表（支持分页 + 模糊搜索）
     * POST /role/queryAll
     */
    @PostMapping("/queryAll")
    public CommonResponse<Map<String, Object>> queryRoleList(@RequestBody Map<String, Object> body) {
        return roleService.queryRoleList(body);
    }

    /**
     * 批量删除角色（基于名称）
     * POST /role/bulkDeleteByNameList
     */
    @PostMapping("/bulkDeleteByNameList")
    public CommonResponse<Map<String, Object>> bulkDeleteByNameList(@RequestBody Map<String, Object> body) {
        return roleService.bulkDeleteByNameList(body);
    }

    /**
     * 编辑角色（基于名称）
     */
    @PostMapping("/updateByName")
    public CommonResponse<Map<String, Object>> updateRoleByName(@RequestBody Map<String, Object> body) {
        return roleService.updateRoleByName(body);
    }
}
